package com.rub.android.jetpack.viewmodel

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import com.rub.android.BR
import com.rub.android.jetpack.model.User
import com.rub.android.utils.logI

/**
 * 双向绑定
 * @Bindable 注解报错解决
 *
 * https://kotlinlang.org/docs/reference/kapt.html
 * https://stackoverflow.com/questions/47044955/kapt-generatestubs-is-not-used-by-the-kotlin-kapt-plugin
 * apply plugin: 'kotlin-kapt'
 * To use data binding annotations in Kotlin, apply the 'kotlin-kapt' plugin in your module's build.gradle
 */
class UserViewModel :BaseObservable{

    private  var user: User

    constructor() {
        this.user = User("蹭蹭")
    }

    @Bindable
    fun getUserName(): String {
        return user.userName
    }

    fun setUserName(userName: String) {
        if (userName != null && user.userName != userName){
            user.userName = userName
            logI("UserViewModel","username:$userName")
           notifyPropertyChanged(BR.userName)
        }

    }
}